#region Imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;
using WorkFlowEngine = Workflow.NET.Engine;
using WorFlowInterface = Workflow.NET.Engine.Interfaces;
using Workflow.NET;
using InsightPolicy = PHSRAG.Insight21.Policy;
using Insight = PHSRAG.Insight21;
using InsightUtilities = PHSRAG.Insight21.InsightUtilities;
using ibex4;
//using xmlpdf;
//using xmlpdf.logging;
//using XSLFo = xslfo;
using SysIO = System.IO;
using SysText = System.Text;
using InsightHumans = PHSRAG.Insight21.Humans; 
using InsightCDS = PHSRAG.Insight21.CDS; 
using InsightUtility = PHSRAG.Utility;
using InsightContext = PHSRAG.Insight21.Context; 
using WorkFlowStorage = Workflow.NET.Storage;
using WorkflowNet = Workflow.NET;
using WorkflowEngine = Workflow.NET.Engine;
using System.Threading;
#endregion

namespace PHSRAG.Insight21.InsightWorkFlow
{
	#region Class PDFCreation
	/// <summary>
	/// This is a Skelta workflow custom action class. This class is creating PDF from the html that gets generated
	/// from Review and Certification page. IActionRun is Skelta interface to implement custom actions. 
	/// Actions.xml of particular application needs to be configured so that this action
	/// becomes accesible from Skelta Workflow Manager control under List of Actions -> Custom Actions. 
	/// This action does not take any parameter. 
	/// </summary>
	public class PDFCreation:Workflow.NET.Engine.Interfaces.IActionRun
	{		
		/// <summary>
		/// Default constructor
		/// </summary>
		public PDFCreation()
		{		
			
		}

		/// <summary>
		/// This custom action takes serialized XML from Skelta variable and creates PDF.
		/// </summary>
		/// <param name="executionId">Workflow execution Id - a unique id used for each workflow item</param>
		/// <param name="executionDetailsId">Execution detail Id - Id used for execution detail table</param>
		/// <param name="currentContext">Context class which encapsulates all the information about the executing Workflow.</param>
		/// <param name="currentAction">Current Workflow action</param>
		/// <param name="inlinkOutput">Output in string format which has come from the step before this step</param>
		/// <param name="retry">Boolean retry variable</param>
		/// <param name="output">Output value which is used by Skelta Workflow manager to takce certain path in the process.</param>
		/// <returns>The result of an action execution. Either execution is complete or action is in sleep mode.  </returns>
		public  WorFlowInterface.ActionResult Run(int executionId,int executionDetailsId,WorkFlowEngine.Context currentContext,Action currentAction,string inlinkOutput,bool retry,out string output)
		{	
			output=null;	
			//Log logger = currentContext.log;
			try
			{
                //System.Diagnostics.Debugger.Launch();
                object insightApplicationName = currentContext.Contents["insightApplicationName"];
                HumansWorkFlow.InsightApplicationName = (null == insightApplicationName) ? string.Empty : ((Content)insightApplicationName).Value.ToString();
                //HumansWorkFlow.InsightApplicationName = currentContext.Contents["insightApplicationName"].Value.ToString();
                Thread.SetData(Thread.GetNamedDataSlot("Connections"), HumansWorkFlow.Connections);
				
//				string html = currentContext.Variables["certificationXML"].Value.ToString(); 
//				string serverPath = currentContext.Variables["serverPath"].Value.ToString(); 
//				string incomingActionName = (string)((Workflow.NET.Property)currentAction.Properties["IncomingActionName"]).Value;
//
//				if (incomingActionName != "")
//				{
//					string respondingUserVar = "^" + incomingActionName + "RespondingUser";
//					WorkflowNet.Resource resource = (WorkflowNet.Resource) (currentContext.Variables[respondingUserVar].Value);
//					int userId = Int32.Parse(resource.Properties["UserID"].Value.ToString());							
//					CreatePDFFile(html,serverPath,Int32.Parse(currentContext.Contents["protocolid"].Value.ToString()),userId);																
//				}
				output = "Success";								
			}
			catch (Exception)
			{								
				//logger.LogMessage(ReasonConst.Error, "Unable to create PDF in PDFCreation.Run() " + ex.StackTrace);  				
				//output = "Failure";						
				output = "Success";	
			}		
			return WorFlowInterface.ActionResult.Completed;  
		}

		/// <summary>
		/// Gets the web control to be displayed in the activity list for this action. No user interface is implemented for
		/// this action. So it returns null.
		/// </summary>
		/// <returns>IActivityWebUIHandler - Interface that allows an object to implement a custom document view Control.</returns>
		public virtual Workflow.NET.Interfaces.IActivityWebUIHandler GetActivityWebUIHandler()
		{
			return null;
		} 
		
	}
	#endregion
}
